<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Aktifitas extends CI_Controller {

    function __construct() 
	{
        parent::__construct();
        $this->load->library('flexigrid');
        $this->load->library('form_validation');
		$this->load->library('session');
        $this->load->helper('flexigrid');
        $this->load->helper(array('form', 'url'));
		// $this->load->model('evaluasi_model');
		$this->load->model('aktifitas_model');
		$this->load->model('proses_aktifitas_model');
        $this->load->model('bagian_jenis_aktifitas_model');
		$this->load->model('user_model');
		$this->cek_session();
    }

    //load Grid
	function cek_session()
	{	
		$kode_role = $this->session->userdata('role_id');
		if($kode_role == '')
		{
			redirect('login');
		}
	}
	
	public function index($id_satuan_jenis_aktifitas)
	{
		$kode_role = $this->session->userdata('role_id');
		
		$hasil = $this->bagian_jenis_aktifitas_model->selectone2($id_satuan_jenis_aktifitas);
		foreach ($hasil->result() as $row) 
		{
            $nama_satuan_jenis_aktifitas = $row->nama_satuan_jenis_aktifitas;
        }
		
		$this->session->set_userdata('id_satuan_jenis_aktifitas', $id_satuan_jenis_aktifitas);
		$this->session->set_userdata('nama_satuan_jenis_aktifitas', $nama_satuan_jenis_aktifitas);				
		
        $colModel['no'] = array('No', 30, TRUE, 'center', 0);
        $colModel['nama_proses_aktifitas'] = array('Nama Aktifitas', 350, TRUE, 'center', 1);
        $colModel['kuota_volume'] = array('Kuota Volume', 80, TRUE, 'center', 1);
		$colModel['satuan'] = array('Satuan', 50, TRUE, 'center', 1);
        $colModel['volume_existing'] = array('Volume sekarang', 90, TRUE, 'center', 1);
		$colModel['persentase_volume'] = array('Persentase Pengerjaan', 130, TRUE, 'center', 1);		   

		//setting konfigurasi pada bottom tool bar flexigrid
        $gridParams = array(
                'width' => 'auto',
                'height' => 300,
                'rp' => 10,
                'rpOptions' => '[5,10,15,20,25,40]',
                'pagestat' => 'Menampilkan: {from} hingga {to} dari {total} data.',
                'blockOpacity' => 0.5,
                'title' => 'Daftar Aktifitas</br>Evaluasi : '.$this->session->userdata('NAMA_EVALUASI').'</br>Bagian Jenis Aktifitas : '.$this->session->userdata('nama_bagian_jenis_aktifitas').'</br>Satuan Jenis Aktifitas : '.$this->session->userdata('nama_satuan_jenis_aktifitas').'',
                'showTableToggleBtn' => true
        );
            									
		//menambah tombol pada flexigrid top toolbar
		if($kode_role == 2 || $kode_role == 4) 
		{			
            $buttons[] = array('Tambah Aktifitas', 'add', 'spt_js');
            $buttons[] = array('separator');
            $buttons[] = array('Perbarui Nilai Aktifitas', 'update', 'spt_js');
            $buttons[] = array('separator');
            $buttons[] = array('Hapus', 'delete', 'spt_js');
			$buttons[] = array('separator');
            $buttons[] = array('Kembali ke Daftar Satuan Aktifitas', 'back', 'spt_js');	
			$buttons[] = array('separator');
		}
		else
		{
			$buttons[] = array('Kembali ke Daftar Satuan Aktifitas', 'back', 'spt_js');	
			$buttons[] = array('separator');
		}
				            		
		// mengambil data dari file controler ajax pada method grid_user				
		$grid_js = build_grid_js('flex1', site_url("aktifitas/grid_foto/".$id_satuan_jenis_aktifitas.""), $colModel, 'nama_proses_aktifitas', 'asc', $gridParams, $buttons);

        $data['added_js'] =
                    "<script type='text/javascript'>
        function spt_js(com,grid)
        {
                if (com=='Tambah Aktifitas')
                {
                        location.href='" . base_url() . "index.php/aktifitas/pilih_aktifitas/".$id_satuan_jenis_aktifitas."';
                }
                
                if (com=='Perbarui Nilai Aktifitas')
                {
                        location.href='" . base_url() . "index.php/aktifitas/form_nilai';
                }
				
				if (com=='Kembali ke Daftar Satuan Aktifitas')
                {
                        location.href='" . base_url() . "index.php/satuan_aktifitas/index/".$this->session->userdata('id_bagian_jenis_aktifitas')."';
                }

                if (com=='Hapus')
                        {
                           if($('.trSelected',grid).length>0){
                                   if(confirm('Anda yakin menghapus ' + $('.trSelected',grid).length + ' aktifitas?')){
                                                var items = $('.trSelected',grid);
                                                var itemlist ='';
                                                for(i=0;i<items.length;i++){
                                                        itemlist+= items[i].id.substr(3)+',';
                                                }
                                                $.ajax({
                                                   type: 'POST',
                                                   url: '" . base_url() . "index.php/aktifitas/delete" . "',
                                                   data: 'items='+itemlist,
                                                   success: function(data){
                                                        $('#flex1').flexReload();
                                                   }
                                                });
                                        }
                                } else {
                                        return false;
                                }
                        }
        }
        </script>
        ";

            $data['js_grid'] = $grid_js;
            $data['content'] = $this->load->view('grid', $data, true);
            $this->load->view('main', $data);				
	}
	
    function grid_foto($id_satuan_jenis_aktifitas) 
	{
        $valid_fields = array('nama_proses_aktifitas', 'kuota_volume', 'satuan', 'volume_existing', 'persentase_volume');
        $this->flexigrid->validate_post('nama_proses_aktifitas', 'asc', $valid_fields);
        $records = $this->aktifitas_model->get_data_proses_aktifitas($id_satuan_jenis_aktifitas);
        $this->output->set_header($this->config->item('json_header'));
        $no = 0;

        foreach ($records['records']->result() as $row) {
            $record_items[] = array(
                $row->id_aktifitas,
                $no = $no + 1,
				$row->nama_proses_aktifitas,
				$row->kuota_volume,
                $row->satuan,			
                $row->volume_existing,
                $row->persentase_volume
            );
        }

        if (isset($record_items))
            $this->output->set_output($this->flexigrid->json_build($records['record_count'], $record_items));
        else
            $this->output->set_output('{"page":"1","total":"0","rows":[]}');
    }
	
	//-------------------------Pilih Aktifitas--------------------//
	
    function pilih_aktifitas($id_satuan_jenis_aktifitas) 
	{
        if ($this->session->userdata('username') == NULL) 
		{
            redirect('login');
        } else {
            $colModel['no'] = array('No', 30, TRUE, 'center', 0);
			$colModel['nama_proses_aktifitas'] = array('Nama Aktifitas', 300, TRUE, 'center', 1);            
			$colModel['satuan'] = array('Satuan', 30, TRUE, 'center', 1);
			$colModel['bagian_proses_aktifitas'] = array('Bagian Proses Aktifitas', 300, TRUE, 'center', 1);			
			
            $nama_satuan_jenis_aktifitas = $this->session->userdata('nama_satuan_jenis_aktifitas');

            $gridParams = array(
                'width' => 'auto',
                'height' => 300,
                'rp' => 10,
                'rpOptions' => '[5,10,15,20,25,40]',
                'pagestat' => 'Menampilkan: {from} hingga {to} dari {total} data.',
                'blockOpacity' => 0.5,
				'title' => 'Daftar Data Aktifitas</br>Evaluasi : '.$this->session->userdata('NAMA_EVALUASI').'</br>Satuan Jenis Aktifitas : '.$nama_satuan_jenis_aktifitas.'',
                'showTableToggleBtn' => true
            );

            $buttons[] = array('Tambahkan aktifitas', 'add', 'spt_js');
			$buttons[] = array('separator');
            $buttons[] = array('Kembali ke daftar aktifitas', 'back', 'spt_js');


            // mengambil data dari file controler ajax pada method grid_berkas
            $grid_js = build_grid_js('flex1', site_url("aktifitas/grid_pilih_proses_aktifitas/".$id_satuan_jenis_aktifitas.""), $colModel, 'nama_proses_aktifitas', 'asc', $gridParams, $buttons);
			
            $data['added_js'] =
                    "<script type='text/javascript'>
        function spt_js(com,grid)
        {
				if (com=='Kembali ke daftar aktifitas')
                {
                        location.href='" . base_url() . "index.php/aktifitas/index/".$this->session->userdata('id_satuan_jenis_aktifitas')."';
                }
				
                if (com=='Tambahkan aktifitas')
                        {
                           if($('.trSelected',grid).length>0){
                                   if(confirm('Tambahkan ' + $('.trSelected',grid).length + ' data aktifitas?')){
                                                var items = $('.trSelected',grid);
                                                var itemlist ='';
                                                for(i=0;i<items.length;i++){
                                                        itemlist+= items[i].id.substr(3)+',';
                                                }
                                                $.ajax({
                                                   type: 'POST',
                                                   url: '" . base_url() . "index.php/aktifitas/add" . "',
                                                   data: 'items='+itemlist,
                                                   success: function(data){
														alert(data);
														window.location.href = '" . base_url() . "index.php/aktifitas/index/".$this->session->userdata('id_satuan_jenis_aktifitas')."';
                                                   }
                                                });
                                        }
                                } else {
                                        return false;
                                }
                        }

        }
        </script>
        ";


            $data['js_grid'] = $grid_js;
            $data['content'] = $this->load->view('grid', $data, true);
            $this->load->view('main', $data);
        }
    }

    function grid_pilih_proses_aktifitas($id_satuan_jenis_aktifitas) 
	{
        $valid_fields = array('nama_proses_aktifitas', 'satuan', 'bagian_proses_aktifitas');
        $this->flexigrid->validate_post('nama_proses_aktifitas', 'asc', $valid_fields);	
        $records = $this->aktifitas_model->get_nama_proses_aktifitas($id_satuan_jenis_aktifitas);
        $this->output->set_header($this->config->item('json_header'));
        $no = 0;

        foreach ($records['records']->result() as $row) 
		{
            $record_items[] = array(
                $row->id_proses_aktifitas,
                $no = $no + 1,
				$row->nama_proses_aktifitas,
				$row->satuan,
                $row->bagian_proses_aktifitas           
            );
        }

        if (isset($record_items))
            $this->output->set_output($this->flexigrid->json_build($records['record_count'], $record_items));
        else
            $this->output->set_output('{"page":"1","total":"0","rows":[]}');
    }
	
	//-------------------------End Pilih Aktifitas--------------------//
	
	
    function add() {
        if ($this->session->userdata('username') == NULL) 
		{
            redirect('login');
        } else {
            $spt_ids_post_array = explode(",", $this->input->post('items'));
			$id_satuan_jenis_aktifitas = $this->session->userdata('id_satuan_jenis_aktifitas');
            foreach ($spt_ids_post_array as $index => $id) {
                if (isset($id) && $id != '') {
					$data = array(
                        'id_proses_aktifitas' => $id,
                        'id_satuan_jenis_aktifitas' => $this->session->userdata('id_satuan_jenis_aktifitas'),
						'ID_EVALUASI' => $this->session->userdata('ID_EVALUASI')						
                    );
					
					//cek data yang sama
					$sql = $this->db->query("SELECT COUNT(id_aktifitas) AS CEK FROM aktifitas WHERE id_proses_aktifitas = ".$id." AND id_satuan_jenis_aktifitas = ".$id_satuan_jenis_aktifitas."");
					$cek = '';
					
					foreach ($sql->result() as $rows) {
						$cek = $rows->CEK;
					}
					
					$msg = '';
					if ($cek > 0) {
						$msg ='Data gagal ditambahkan. Data aktifitas sudah ada.';
					}
					else{
						$this->aktifitas_model->insert($data);
						$msg ='Data aktifitas berhasil ditambahkan.';
					}
					$this->output->set_header($this->config->item('ajax_header'));
					$this->output->set_output($msg);
                }//end if
            }//end foreach
        }
    }
        
    public function form_nilai()
	{
		$data['status'] = 'new';
		$data['failed'] = false;
		$data['aa'] = '';
		$data['data_form_nilai'] = $this->aktifitas_model->data_form_nilai($this->session->userdata('id_satuan_jenis_aktifitas'));
		$data['content'] = $this->load->view('form_sk', $data, true);
		$this->load->view('main', $data);
	}   
	
	function save_nilai()
	{
		$data_form_nilai = $this->aktifitas_model->data_form_nilai($this->session->userdata('id_satuan_jenis_aktifitas'));
		foreach ($data_form_nilai->result() as $row)
		{
			$data = array(
				'kuota_volume' => $this->input->post('kuota_volume'.$row->id_proses_aktifitas.''),
				'volume_existing' => $this->input->post('volume_existing'.$row->id_proses_aktifitas.''),
				'persentase_volume' => $this->input->post('persentase_volume'.$row->id_proses_aktifitas.'')
			);
			$this->aktifitas_model->save_nilai($row->id_proses_aktifitas, $row->id_satuan_jenis_aktifitas,$data);
		}
		redirect('aktifitas/index/'.$this->session->userdata('id_satuan_jenis_aktifitas'));
	}   	

    function delete() 
	{
        if ($this->session->userdata('username') == NULL) 
		{
            redirect('login');
        } else {
            $spt_ids_post_array = explode(",", $this->input->post('items'));
  
            foreach ($spt_ids_post_array as $index => $id) 
			{
                if (isset($id) && $id != '') 
				{
                    $this->aktifitas_model->delete($id);
                }//end if
            }//end foreach
        }
    }

    function cek_validasi() 
	{
        // Setting Rules
        $this->form_validation->set_rules('nama_proses_aktifitas', 'Nama Aktifitas', 'required');
        $this->form_validation->set_rules('satuan', 'Satuan', 'required');

        //Setting Error Message
        $this->form_validation->set_message('required', 'Field %s harus diisi.');

        // Setting Delimiter
        $this->form_validation->set_error_delimiters('<li class="error">', '</li>');
        return $this->form_validation->run();
    }

	

}
